package com.lintf.distributed.transaction.inventory.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lintf.distributed.transaction.inventory.entity.Inventory;
import com.lintf.distributed.transaction.inventory.exception.BusinessException;
import com.lintf.distributed.transaction.inventory.mapper.InventoryMapper;
import com.lintf.distributed.transaction.inventory.service.InventoryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author lintf
 * @description 针对表【t_inventory】的数据库操作Service实现
 * @createDate 2025-06-13 10:28:04
 */
@Service
public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory>
        implements InventoryService {

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public boolean deductStock(Long productId, Integer quantity) {
        LambdaUpdateWrapper<Inventory> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Inventory::getProductId, productId)
                .ge(Inventory::getStock, quantity)
                .setSql("stock = stock - " + quantity);
        int update = this.baseMapper.update(null, updateWrapper);
        return update != 0;
    }

}




